package com.shopping.controller;

import com.shopping.bean.User;
import com.shopping.service.IUserService;
import com.shopping.util.LayData;
import com.shopping.util.Page;
import com.shopping.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    IUserService userService;
    @RequestMapping("getuserinfo")
    @ResponseBody
    public LayData getLoginUser(HttpSession session){
        User loginuser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginuser != null) {
            System.out.println("进入 user/getuserinfo 接口，从会话中获取的用户 userid: " + loginuser.getUserid());
        } else {
            System.out.println("进入 user/getuserinfo 接口，会话中用户信息为空");
        }
        int id = loginuser != null ? loginuser.getUserid() : 0;
        User user = userService.getUserByID(id);
        if (loginuser != null) {
            System.out.println("离开 user/getuserinfo 接口，会话中的用户 userid: " + loginuser.getUserid());
        } else {
            System.out.println("离开 user/getuserinfo 接口，会话中用户信息为空");
        }
        return LayData.success(200, "", user);
    }
    @RequestMapping("edit")
    @ResponseBody
    public LayData editUser(HttpSession session,User user){
        if (user.getUserid()==0) {
            User loginuser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
            int id = loginuser.getUserid();
            user.setUserid(id);
        }
            userService.editUser(user);
            return LayData.success(200,null);

        }
    @RequestMapping("getUserByUsername")
    @ResponseBody
        public LayData getUserByUsername(User user){
        User userData=userService.getUser(user);
        return LayData.success(userData);
        }

    @RequestMapping("register")
    @ResponseBody
    public LayData register(User user,String confirmpassword){
        //验证格式是否正确
        boolean check=SYS.check("^\\w{6,16}$",user.getUsername(),user.getPassword(),confirmpassword);
        if (!check){
            return LayData.success(400,"输入的内容格式不正确");
        }
        if (!confirmpassword.equals(user.getPassword())){
            return LayData.success(400,"两次输入的密码不一样");
        }
        User userData=userService.getUser(user);
        if (userData!=null){
            return LayData.success(400,"账号已存在");
        }
        String hexpass=SYS.digest(user.getPassword());
        user.setPassword(hexpass);
        int count=userService.add(user);
        return LayData.success(200,"注册成功",userData);
    }
    @RequestMapping("delete")
    @ResponseBody
    public LayData deleteUser(@RequestParam("id") Integer userid) {
        int result = userService.deleteUser(userid);
        if (result > 0) {
            return LayData.success(200, "删除成功");
        } else {
            return LayData.success(400, "删除失败");
        }
    }

    @RequestMapping("list")
    @ResponseBody
    public LayData list(Page page,String text){
        List<User> list=userService.getUserList(text,page);
        int count=userService.getUserCount(text);
        return new LayData(count,list);
    }



    @RequestMapping("updatePassword")
    @ResponseBody
    public LayData updatePassword(HttpSession session,
                                  @RequestParam("oldpassword") String oldpassword,
                                  @RequestParam("newpassword") String newpassword) {
        User loginuser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginuser == null) {
            return LayData.success(400, "用户未登录");
        }
        int userId = loginuser.getUserid();
        // 验证旧密码
        String storedPassword = loginuser.getPassword();
        if (!SYS.checkPass(oldpassword, storedPassword)) {
            return LayData.success(400, "旧密码输入错误");
        }
        int result = userService.updatePassword(userId, SYS.digest(newpassword));
        if (result > 0) {
            return LayData.success(200, "密码修改成功");
        } else {
            return LayData.success(400, "密码修改失败");
        }
    }

}
